#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

class Solution
{
public:
    void mpsort(vector<pair<int, int>> mp)
    {


    }

    vector<int> kWeakestRows(vector<vector<int>>& mat, int k)
    {
        int cnt;
        int l = -1;
        vector<pair<int, int>> mp;
        
        for (auto i : mat)
        {
            l++;
            cnt = 0;
            for (auto j : i)
            {                
                cnt++;
                if (j == 0)
                {
                    cnt--;
                    break;
                }
            }
            mp.emplace_back(l, cnt);
        }
        //mpsort(mp);
        sort(mp.begin(), mp.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.second == b.second ? a.first < b.first : a.second < b.second;
            });

        vector<int> v;
        pair<int, int> tmp;
        for (auto e : mp)
        {
            if (k == 0)
                break;
            
            k--;
            v.emplace_back(e.first);
            
            tmp = e;
            //cout << e.first << " " << e.second << endl;
        }
        return v;
    }
};

int main()
{
    vector<vector<int>> mat{ {1,1,0,0,0},{1,1,1,1,0},{1,0,0,0,0},{1,1,0,0,0},{1,1,1,1,1} };
    int k = 3;

    Solution xz;
    vector<int> v = xz.kWeakestRows(mat, k);

    for (auto e : v)
    {
        cout << e << ' ';
    }

	return 0;
}